package com.choudou5.solr.utils;

import cn.hutool.core.collection.CollUtil;
import com.choudou5.solr.bean.common.SelectBean;
import com.choudou5.solr.framework.bean.UserBean;
import com.choudou5.solr.framework.util.UserUtil;
import com.choudou5.solr.framework.util.cache.CacheHelper;
import com.choudou5.solr.framework.util.cache.EhCacheHelper;
import com.choudou5.solr.service.ServiceHelper;
import com.choudou5.solr.service.SysUserService;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @name UserUtils 说明
 * @author choudou5
 * @date：2018-07-26
 */
public class UserUtils extends UserUtil {

    private static final CacheHelper cacheHelper = EhCacheHelper.getInstance();

    private static final SysUserService userService = ServiceHelper.singleton(SysUserService.class);

    /**
     * 获取用户 姓名
     * @param userId
     * @return
     */
    public static String getUserName(Integer userId) {
        if(userId == null)
            return null;
        String cacheKey = "userName_"+userId;
        String userName = cacheHelper.get(cacheKey);
        if(userName == null){
            UserBean user = userService.get(userId);
            if(user != null) {
                userName = user.getName();
            }else{
                userName = "";
            }
            cacheHelper.put(cacheKey, userName);
        }
        return StringUtils.replace(userName, "\"", "");
    }

    /**
     * 获取 用户下拉列表数据
     * @return
     */
    public static List<SelectBean> getUserSelectList(){
        List<UserBean> userBeans = userService.getSelectListAll();
        List<SelectBean> result = new ArrayList<>();
        if(CollUtil.isNotEmpty(userBeans)){
            for (UserBean userBean : userBeans) {
                result.add(new SelectBean(userBean.getName(), userBean.getId().toString()));
            }
        }
        return result;
    }

}
